var cert = {
	init: function() {
		layui.use(['layer', 'form','laydate', 'table','util', 'admin', 'utilPage','element'], function() {
			var $ = layui.jquery;
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var util = layui.util;
			var admin = layui.admin;
			var utilPage = layui.utilPage;
			var element = layui.element;
			var laydate = layui.laydate;
			/* 渲染表格 */
			var insTb = table.render({
				elem: '#baseTable',
				url: '/sys/cert/getPage',
				method: 'post',
				page: true,
				toolbar: '#tableToolBar',
				request: {
					pageName: 'pageNum', //页码的参数名称，默认：page
					limitName: 'pageSize' //每页数据量的参数名，默认：limit
				},
				parseData: function(res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": res.msg, //解析提示文本
						"count": res.page.total, //解析数据长度
						"data": res.page.records //解析数据列表
					};
				},
				cellMinWidth: 100,
				cols: [
					[
						{
							type: 'checkbox'
						},
						{
							type: 'numbers'
						},
						{
							field: 'name',
							title: '姓名',
							width: 100
						},
						{
							field: 'cardNumber',
							title: '身份证号',
							width: 180
						},
						{
							field: 'major',
							title: '专业,课题',
							width: 417
						},
						{
							field: 'period',
							title: '课时',
							width: 60
						},
						{
							field: 'certPrefix',
							title: '证书编号'
						},
						{
							title: '操作',
							toolbar: '#cellBar',
							align: 'center',
							minWidth: 200
						}
					]
				]
			});



			/* 表格搜索 */
			form.on('submit(tbSearch)', function(data) {
				console.log("表格搜索------")
				console.log(data)
				insTb.reload({
					where: data.field,
					page: {
						curr: 1
					}
				});
				return false;
			});



			//点击弹出导入Excel的模态框
			$('#showExcel').on("click",function (){
				console.log("进入---弹出模态框函数")
				layer.open({
					area: ['500px','300px'],
					//点击旁边地方自动关闭
					shadeClose: true,
					anim:2,
					type:1,
					content:$('#showImport').html(),
					end: function () {
						insTb.reload({
							where: {},
							page: {
								curr: 1
							}
						});
					}

				})
			})


			//上传Excel
			form.on('submit(certSubmit)',function (obj){
				console.log("123123")
				var formData = new FormData($("#formData")[0]);
				console.log(formData)
				$.ajax({
					type: "POST",           //因为是传输文件，所以必须是post
					url: 'http://localhost:8090/sys/cert/excel',         //对应的后台处理类的地址
					data: formData,
					processData: false,
					contentType: false,
					loading: true,
					dataType:'json',
					beforeSend: function () {
						this.layerIndex = layer.load(0, { shade: [0.5, '#7f7766'] });
						//此处是摇头啦 转圈拉 等待的拉
					},

					success: function(res){
						console.log(res);
						if (res.code == 200) {
							layer.msg(res.msg, {
								icon: 1
							});


						} else {
							layer.msg(res.msg, {
								icon: 2
							});
						}

					},
					complete: function () {
						layer.close(this.layerIndex);
						//关闭遮罩层
					},
					error:function (err){
						alert("导入出错！！")
					}
				});
			})


			//下载模板
			$("#download").on("click",function (){
				//cert模板下载
				window.open("/static/excelmould/cert.xlsx")
			})






			/* 表格工具条点击事件 */
			table.on('tool(baseTable)', function(obj) {

				if (obj.event === 'edit') { // 修改
					showEditModel(obj.data);
				} else if (obj.event === 'del') { // 删除
					doDel(obj);
				} else if (obj.event === 'preview') { // 预览
					preview(obj);
				}
			});



			//预览证书
			table.on("cellBar(preview)",function (obj){
				layer.open({
					area: ['100%','100%'],
					//点击旁边地方自动关闭
					// shadeClose: true,
					anim: 2,
					type: 1,
					content: "/templates/cert/cert.ftl",
					end: function () {
						insTb.reload({
							where: {},
							page: {
								curr: 1
							}
						});
					}

				})
			})



			//点击预览函数
			function preview(obj){
				var certid = obj.data.id;
				console.log(certid)
				$.ajax({
					type: 'POST',
					data: certid,
					dataType: 'html',
					url: '/sys/cert/preview/'+certid,
					//返回一个页面下面很重要
					success(res){
						document.write(res)  //一句话搞定返回页面
					}
				})
			}



			/* 表格头工具栏点击事件 */
			table.on('toolbar(baseTable)', function(obj) {
				if (obj.event === 'add') { // 添加
					showEditModel();
				} else if (obj.event === 'del') { // 删除
					var checkRows = table.checkStatus('baseTable');
					if (checkRows.data.length === 0) {
						layer.msg('请选择要删除的数据', {
							icon: 2
						});
						return;
					}
					var ids = checkRows.data.map(function(d) {
						return d.id;
					});

					console.log("ids:"+ids);

					doDel({
						ids: ids
					});
				}else if (obj.event === 'recycle') { //回收站
					var openWH = utilPage.getOpenWidthHeight();
					var index = admin.open({
						type: 2,
						title: '回收站',
						content: '/sys/cert/recycleIndex',
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area: ['100%', '100%'],
						end: function(layero, index){
							insTb.reload();
						}
					});
					layer.full(index);
				}

			});




			/* 修改证书状态 */
			form.on('switch(certTbStateCk)', function(obj) {
				// var loadIndex = layer.load(2);
				$.ajax({
					url: '/sys/cert/uptStatus',
					data: {
						id: obj.elem.value,
						status: obj.elem.checked ? 0 : 1
					},
					type: 'post',
					dataType: 'json',
					success: function(res) {
						layer.close(loadIndex);
						if (res.code == 200) {
							layer.msg(res.msg, {
								icon: 1
							});
						} else {
							layer.msg(res.msg, {
								icon: 2
							});
							$(obj.elem).prop('checked', !obj.elem.checked);
							form.render('checkbox');
						}
					},
					error: function(xhr, status, error) {
						layer.close(loadIndex);
						var res = xhr.responseJSON;
						layer.msg(res.msg, {
							icon: 2,
							anim: 6
						});
					}
				});
			});




			/* 显示表单弹窗 */
			function showEditModel(mData) {
				admin.open({
					area: ['600px','680px'],
					type: 1,
					title: (mData ? '修改' : '添加') + '证书',
					content: $('#editDialog').html(),
					success: function(layero, dIndex) {
						if(mData){
							mData.startTime = mData.startTime ? util.toDateString(mData.startTime) : "";
							mData.endTime = mData.endTime ? util.toDateString(mData.endTime) : "";
							mData.licenceIssued = mData.licenceIssued ? util.toDateString(mData.licenceIssued) : "";
						}
						form.val('userEditForm', mData);
						// 表单提交事件
						form.on('submit(userEditSubmit)', function(data) {
							var loadIndex = layer.load(2);
							$.ajax({
								url: mData ? '/sys/cert/update' : '/sys/cert/add',
								data: data.field,
								type: 'post',
								dataType: 'json',
								success: function(res) {

									laydate.render({
										elem: '#startTime',
										trigger: 'click',
										type: 'date'
									});
									laydate.render({
										elem: '#endTime',
										trigger: 'click',
										type: 'date'
									});
									laydate.render({
										elem: '#licenceIssued',
										trigger: 'click',
										type: 'date'
									});

									layer.close(loadIndex);
									if (res.code == 200) {
										layer.close(dIndex);
										layer.msg(res.msg, {
											icon: 1
										});
										insTb.reload({
											page: {
												curr: 1
											}
										});

									} else {
										layer.msg(res.msg, {
											icon: 2
										});
									}
								},
								error: function(xhr, status, error) {
									layer.close(loadIndex);
									var res = xhr.responseJSON;
									layer.msg(res.msg, {
										icon: 2,
										anim: 6
									});
								}
							});
							return false;
						});
						// 禁止弹窗出现滚动条
						$(layero).children('.layui-layer-content').css('overflow', 'visible');
					}
				});
			}



			/* 删除 */
			function doDel(obj) {
				layer.confirm('确定要删除选中数据吗？', {
					skin: 'layui-layer-admin',
					shade: .1
				}, function(i) {
					layer.close(i);
					var loadIndex = layer.load(2);
					$.ajax({
						url: '/sys/cert/recycle',
						data: {
							ids: obj.ids ? obj.ids.join(',') : (obj.data.id ? obj.data.id : '')
						},
						type: 'post',
						dataType: 'json',
						success: function(res) {
							layer.close(loadIndex);
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1
								});
								insTb.reload({
									page: {
										curr: 1
									}
								});

							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						},
						error: function(xhr, status, error) {
							layer.close(loadIndex);
							var res = xhr.responseJSON;
							layer.msg(res.msg, {
								icon: 2,
								anim: 6
							});
						}
					});
				});



			}
		});
	},

	initRecycle: function() {
		layui.use(['layer', 'form', 'table', 'util', 'admin'], function() {
			var $ = layui.jquery;
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var util = layui.util;
			var admin = layui.admin;
			/* 渲染表格 */
			var insTb = table.render({
				elem: '#baseTable',
				url: '/sys/cert/getRecyclePage',
				method: 'post',
				page: true,
				toolbar: '#tableToolBar',
				request: {
					pageName: 'pageNum', //页码的参数名称，默认：page
					limitName: 'pageSize' //每页数据量的参数名，默认：limit
				},
				parseData: function(res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": res.msg, //解析提示文本
						"count": res.page.total, //解析数据长度
						"data": res.page.records //解析数据列表
					};
				},
				cellMinWidth: 100,
				cols: [
					[
						{
							type: 'checkbox'
						},
						{
							type: 'numbers'
						},
						{
							field: 'name',
							title: '姓名',
							width: 100
						},
						{
							field: 'cardNumber',
							title: '身份证号',
							width: 240
						},
						{
							field: 'major',
							title: '专业,课题',
							width: 280
						},
						{
							field: 'period',
							title: '课时',
							width: 50
						},
						{
							field: 'certPrefix',
							title: '证书编号'
						},

						{
							field: 'licenceIssued',
							title: '发证日期',
							templet:function (d){
								return util.toDateString(d.licenceIssued)
							}
						},
						{
							field: 'status',
							title: '状态',
							templet: '#certTbState',
						},
						{
							title: '操作',
							toolbar: '#cellBar',
							align: 'center',
							minWidth: 200
						}
					]
				]
			});

			/* 回收站表格搜索 */
			form.on('submit(tbSearch)', function(data) {
				insTb.reload({
					where: data.field,
					page: {
						curr: 1
					}
				});
				return false;
			});



			/* 回收站表格工具条点击事件 */
			table.on('tool(baseTable)', function(obj) {
				if (obj.event === 'restore') { // 还原
					doRestore(obj);
				} else if (obj.event === 'del') { // 删除
					doDel(obj);
				}
			});

			/* 回收站表格头工具栏点击事件 */
			table.on('toolbar(baseTable)', function(obj) {
				if (obj.event === 'restore') { // 还原
					var checkRows = table.checkStatus('baseTable');
					if (checkRows.data.length === 0) {
						layer.msg('请选择要还原的数据', {
							icon: 2
						});
						return;
					}
					var ids = checkRows.data.map(function(d) {
						return d.id;
					});
					doRestore({
						ids: ids
					});
				} else if (obj.event === 'del') { // 删除
					var checkRows = table.checkStatus('baseTable');
					if (checkRows.data.length === 0) {
						layer.msg('请选择要删除的数据', {
							icon: 2
						});
						return;
					}
					var ids = checkRows.data.map(function(d) {
						return d.id;
					});
					doDel({
						ids: ids
					});
				}
			});

			/* 回收站还原 */
			function doRestore(obj) {
				layer.confirm('确定要还原选中数据吗？', {
					skin: 'layui-layer-admin',
					shade: .1
				}, function(i) {
					layer.close(i);
					var loadIndex = layer.load(2);
					$.ajax({
						url: '/sys/cert/restore',
						data: {
							ids: obj.ids ? obj.ids.join(',') : (obj.data.id ? obj.data.id : '')
						},
						type: 'post',
						dataType: 'json',
						success: function(res) {
							layer.close(loadIndex);
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1
								});
								insTb.reload({
									page: {
										curr: 1
									}
								});

							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						},
						error: function(xhr, status, error) {
							layer.close(loadIndex);
							var res = xhr.responseJSON;
							layer.msg(res.msg, {
								icon: 2,
								anim: 6
							});
						}
					});
				});
			}



			/* 回收站删除 */
			function doDel(obj) {
				console.log("进入回收站删除方法")
				layer.confirm('确定要彻底删除选中数据吗？', {
					skin: 'layui-layer-admin',
					shade: .1
				}, function(i) {
					layer.close(i);
					var loadIndex = layer.load(2);
					$.ajax({
						url: '/sys/cert/delete',
						data: {
							ids: obj.ids ? obj.ids.join(',') : (obj.data.id ? obj.data.id : '')
						},
						type: 'post',
						dataType: 'json',
						success: function(res) {
							layer.close(loadIndex);
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1
								});
								insTb.reload({
									page: {
										curr: 1
									}
								});

							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						},
						error: function(xhr, status, error) {
							layer.close(loadIndex);
							var res = xhr.responseJSON;
							layer.msg(res.msg, {
								icon: 2,
								anim: 6
							});
						}
					});
				});
			}





		});
	}
}
